{% set notifications = [] %}
{% for item in userNotifications %}
    {% if item.notification.context.gridParameters is defined and item.notification.route is not null %}
        {% set generatedUrl = path(item.notification.route) ~ '|g/' ~ item.notification.context.gridParameters %}
    {% elseif item.notification.route is not null %}
        {% set generatedUrl = path(item.notification.route, item.notification.routeParams) %}
    {% else %}
        {% set generatedUrl = null %}
    {% endif %}

    {% set sanitizedMessageParams = {} %}
    {% for labelKey, value in item.notification.messageParams %}
        {% set sanitizedMessageParams = sanitizedMessageParams|merge({('' ~ labelKey): value|escape}) %}
    {% endfor %}

    {% set notifications = notifications|merge([{
        id: item.id,
        type: item.notification.type,
        message: item.notification.message|trans(sanitizedMessageParams),
        viewed: item.viewed,
        url: generatedUrl,
        createdAt: item.notification.created|date("Y-m-d H:i:s", userTimezone),
        actionType: item.notification.context.actionType ? item.notification.context.actionType : null,
        buttonLabel: item.notification.context.buttonLabel is defined ? item.notification.context.buttonLabel|trans : 'pim_notification.report'|trans,
        showReportButton: item.notification.context.showReportButton is defined ? item.notification.context.showReportButton : true,
        comment: item.notification.comment ? item.notification.comment : null
    }]) %}
{% endfor %}
{% set data = { notifications: notifications, unreadCount: notification_count() } %}
{{ data|json_encode|raw }}
